package com.ykm.testapp;

import android.app.Application;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;

import io.realm.Realm;
import io.realm.RealmConfiguration;

/**
 * Created by ykm on 2017/10/9.
 */

public class TestApp extends Application {
    private static final String TAG = "TestApp";
    private static TestApp instance;

    public static TestApp getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        init();
        Logger.d("初始化App");
//        Test.getInstance().testHawk();
//        Test.getInstance().testlog();
//        Test.getInstance().realmAdd();
//        Test.getInstance().deleteFromRealm();
//        Test.getInstance().realmWhere();

    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    //初始化工具
    private void init() {
        //初始化logger
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)  // (Optional) Whether to show thread info or not. Default true
                .methodCount(1)         // (Optional) How many method line to show. Default 2
                //.methodOffset(0)        // (Optional) Hides internal method calls up to offset. Default 0
                //.logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
                .tag(TAG)   // (Optional) Global tag for every log. Default PRETTY_LOGGER
                .build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                // 返回是否打印日志
                return true;
            }
        });

        //初始化Hawk:加密简单存储数据
        Hawk.init(this).build();

        //初始化Realm：加密数据库，多平台，速度快
        Realm.init(this);
//        Realm realm = Realm.getDefaultInstance();
//        Logger.d("realm path: %s   version: %d", realm.getPath(), realm.getVersion());

        RealmConfiguration config = new RealmConfiguration.Builder()
                .name("TestApp.realm")
                .deleteRealmIfMigrationNeeded()
                .build();
        Realm.setDefaultConfiguration(config);
//        Realm realm = Realm.getDefaultInstance();
//        Logger.d("realm path: %s   version: %d", realm.getPath(), realm.getVersion());

//        RealmConfiguration myConfig = new RealmConfiguration.Builder()
//                .name("myrealm.realm")
//                .deleteRealmIfMigrationNeeded()
//                .build();
//        Realm myRealm = Realm.getInstance(myConfig);
//        Logger.d("myRealm path: %s   version: %d", myRealm.getPath(), myRealm.getVersion());

//        RealmConfiguration otherConfig = new RealmConfiguration.Builder()
//                .name("otherrealm.realm")
//                .deleteRealmIfMigrationNeeded()
//                .build();
//        Realm otherRealm = Realm.getInstance(otherConfig);
//        Logger.d("otherRealm path: %s   version: %d", otherRealm.getPath(), otherRealm.getVersion());

    }
}
